clear all

use "$root\data\use.dta", replace


**# Alternative drought measures
* Regressions
foreach crop in corn soybeans {
	foreach var in pdsi pmdi phdi {
		preserve
		local c = cond("`crop'" == "corn", "c", "s")
		rename `c'_d*_* d*_*
		rename `c'_pdsi_* pdsi_*
		rename `c'_phdi_* phdi_*
		rename `c'_pmdi_* pmdi_*
		rename `c'_tbin*_* tbin*_*
		rename `c'_prec_* prec_*

		reghdfe ln`crop'_pplant `var'_b `var'_p if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_pplanted_wo
	
		reghdfe ln`crop'_pplant `var'_b `var'_p tbin*_b prec_b* tbin*_p prec_p* if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_pplanted_w 

		reghdfe ln`crop'_pplant `var'_b `var'_p tbin*_b `var'_w_b tbin*_p `var'_w_p if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_pplanted_h // note the name change

		reghdfe `crop'_plratio `var'_b `var'_p if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_plratio_wo

		reghdfe `crop'_plratio `var'_b `var'_p tbin*_b prec_b* tbin*_p prec_p* if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_plratio_w
	
		reghdfe `crop'_plratio `var'_b `var'_p tbin*_b `var'_w_b tbin*_p `var'_w_p if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_plratio_h // note the name change
		
		reghdfe `crop'_hratio `var'_b `var'_p `var'_g if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_hratio_wo

		reghdfe `crop'_hratio `var'_b `var'_p `var'_g tbin*_b prec_b* tbin*_p prec_p* tbin*_g prec_g* if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_hratio_w
			
		reghdfe `crop'_hratio `var'_b `var'_p `var'_g tbin*_b `var'_w_b tbin*_p `var'_w_p tbin*_g `var'_w_g if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
		estimate store `crop'_`var'_hratio_h // note the name change

		foreach outcome in yield production {
			reghdfe ln`crop'_`outcome' `var'_b `var'_p `var'_g if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
			estimate store `crop'_`var'_`outcome'_wo

			reghdfe ln`crop'_`outcome' `var'_b `var'_p `var'_g tbin*_b prec_b* tbin*_p prec_p* tbin*_g prec_g* if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
			estimate store `crop'_`var'_`outcome'_w
			
			reghdfe ln`crop'_`outcome' `var'_b `var'_p `var'_g tbin*_b `var'_w_b tbin*_p `var'_w_p tbin*_g `var'_w_g if irrigated_ratio<0.1&N_year_`crop'>10&lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
			estimate store `crop'_`var'_`outcome'_h // note the name change
		
		}
			
	restore
	}
}

**## Plots
* Planned
coefplot (corn_pdsi_pplanted_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black) )) /// 
	(corn_pdsi_pplanted_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) )  msymbol(T)) ///
	(corn_pdsi_pplanted_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(corn_pmdi_pplanted_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black) )) /// 
	(corn_pmdi_pplanted_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) )  msymbol(T)) ///
	(corn_pmdi_pplanted_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(corn_phdi_pplanted_wo, label(No Weather Controls) offset(-0.2) ciopts(color(black) )) /// 
	(corn_phdi_pplanted_w, label(Temp. and Precip. Controls) offset(0) ciopts(color(black) )  msymbol(T)) ///
	(corn_phdi_pplanted_h, label(Temp. and Wetness Controls) offset(0.2) ciopts(color(black)) msymbol(S)) ///
	, bylabel(Corn) || ///
	(soybeans_pdsi_pplanted_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black)  )) /// 
	(soybeans_pdsi_pplanted_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_pdsi_pplanted_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(soybeans_pmdi_pplanted_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black)  )) /// 
	(soybeans_pmdi_pplanted_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_pmdi_pplanted_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(soybeans_phdi_pplanted_wo, label(No Weather Controls) offset(-0.2) ciopts(color(black)  )) /// 
	(soybeans_phdi_pplanted_w, label(Temp. and Precip. Controls) offset(0) ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_phdi_pplanted_h, label(Temp. and Wetness Controls) offset(0.2) ciopts(color(black)) msymbol(S)) ///
	, bylabel(Soybeans) ||, ///
    mcolor(black) mfcolor(white) vertical keep(p*i_b p*i_p) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(pdsi_* = "PDSI" pmdi_* = "PMDI" phdi_* = "PHDI")  ytitle("ln(Planned Acres)") ylab(,labs(small)) ///
	xlabel(1 "Pre-planting" 2 "Planting" 4 "Pre-planting" 5 "Planting" 7 "Pre-planting" 8 "Planting")	
graph export "$root/results_replication/appendix_figures/afig7_1_alt_pplanted.png", as(png) name("Graph") width(3200) height(2400) replace

* Plant Ratio	
coefplot (corn_pdsi_plratio_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black) )) /// 
	(corn_pdsi_plratio_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) )  msymbol(T)) ///
	(corn_pdsi_plratio_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(corn_pmdi_plratio_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black) )) /// 
	(corn_pmdi_plratio_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) )  msymbol(T)) ///
	(corn_pmdi_plratio_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(corn_phdi_plratio_wo, label(No Weather Controls) offset(-0.2) ciopts(color(black) )) /// 
	(corn_phdi_plratio_w, label(Temp. and Precip. Controls) offset(0) ciopts(color(black) )  msymbol(T)) ///
	(corn_phdi_plratio_h, label(Temp. and Wetness Controls) offset(0.2) ciopts(color(black)) msymbol(S)) ///
	, bylabel(Corn) || ///
	(soybeans_pdsi_plratio_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black)  )) /// 
	(soybeans_pdsi_plratio_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_pdsi_plratio_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(soybeans_pmdi_plratio_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black)  )) /// 
	(soybeans_pmdi_plratio_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_pmdi_plratio_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(soybeans_phdi_plratio_wo, label(No Weather Controls) offset(-0.2) ciopts(color(black)  )) /// 
	(soybeans_phdi_plratio_w, label(Temp. and Precip. Controls) offset(0) ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_phdi_plratio_h, label(Temp. and Wetness Controls) offset(0.2) ciopts(color(black)) msymbol(S)) ///
	, bylabel(Soybeans) ||, ///
	mcolor(black) mfcolor(white) vertical keep(p*i_b p*i_p) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(pdsi_* = "PDSI" pmdi_* = "PMDI" phdi_* = "PHDI")  ytitle("Planted Ratio") ylab(,labs(small)) ///
	xlabel(1 "Pre-planting" 2 "Planting" 4 "Pre-planting" 5 "Planting" 7 "Pre-planting" 8 "Planting")	
graph export "$root/results_replication/appendix_figures/afig7_2_alt_plratio.png", as(png) name("Graph") width(3200) height(2400) replace

* Harvest Ratio	
coefplot (corn_pdsi_hratio_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black) )) /// 
	(corn_pdsi_hratio_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) )  msymbol(T)) ///
	(corn_pdsi_hratio_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(corn_pmdi_hratio_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black) )) /// 
	(corn_pmdi_hratio_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) )  msymbol(T)) ///
	(corn_pmdi_hratio_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(corn_phdi_hratio_wo, label(No Weather Controls) offset(-0.2) ciopts(color(black) )) /// 
	(corn_phdi_hratio_w, label(Temp. and Precip. Controls) offset(0) ciopts(color(black) )  msymbol(T)) ///
	(corn_phdi_hratio_h, label(Temp. and Wetness Controls) offset(0.2) ciopts(color(black)) msymbol(S)) ///
	, bylabel(Corn) || ///
	(soybeans_pdsi_hratio_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black)  )) /// 
	(soybeans_pdsi_hratio_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_pdsi_hratio_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(soybeans_pmdi_hratio_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black)  )) /// 
	(soybeans_pmdi_hratio_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_pmdi_hratio_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(soybeans_phdi_hratio_wo, label(No Weather Controls) offset(-0.2) ciopts(color(black)  )) /// 
	(soybeans_phdi_hratio_w, label(Temp. and Precip. Controls) offset(0) ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_phdi_hratio_h, label(Temp. and Wetness Controls) offset(0.2) ciopts(color(black)) msymbol(S)) ///
	, bylabel(Soybeans) ||, ///
	mcolor(black) mfcolor(white) vertical keep(*i_b *i_p *i_g) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(pdsi_* = "PDSI" pmdi_* = "PMDI" phdi_* = "PHDI")  ytitle("Harvested Ratio") ylab(,labs(small)) ///
	xlabel(1 "Pre-planting" 2 "Planting" 3 "Growing" 5 "Pre-planting" 6 "Planting" 7 "Growing" 9 "Pre-planting" 10 "Planting" 11 "Growing", labs(small))		
graph export "$root/results_replication/appendix_figures/afig7_3_alt_hratio.png", as(png) name("Graph") width(3200) height(2400) replace

* Yield, Production
foreach y in yield production {
	local y_name = strproper("`y'")
	local order = cond("`y'" == "yield", 4, 5)
	coefplot (corn_pdsi_`y'_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black) )) /// 
	(corn_pdsi_`y'_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) )  msymbol(T)) ///
	(corn_pdsi_`y'_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(corn_pmdi_`y'_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black) )) /// 
	(corn_pmdi_`y'_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) )  msymbol(T)) ///
	(corn_pmdi_`y'_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(corn_phdi_`y'_wo, label(No Weather Controls) offset(-0.2) ciopts(color(black) )) /// 
	(corn_phdi_`y'_w, label(Temp. and Precip. Controls) offset(0) ciopts(color(black) )  msymbol(T)) ///
	(corn_phdi_`y'_h, label(Temp. and Wetness Controls) offset(0.2) ciopts(color(black)) msymbol(S)) ///
	, bylabel(Corn) || ///
	(soybeans_pdsi_`y'_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black)  )) /// 
	(soybeans_pdsi_`y'_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_pdsi_`y'_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(soybeans_pmdi_`y'_wo, label(No Weather Controls) offset(-0.2) nokey ciopts(color(black)  )) /// 
	(soybeans_pmdi_`y'_w, label(Temp. and Precip. Controls) offset(0) nokey ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_pmdi_`y'_h, label(Temp. and Wetness Controls) offset(0.2) nokey ciopts(color(black)) msymbol(S)) ///
	(soybeans_phdi_`y'_wo, label(No Weather Controls) offset(-0.2) ciopts(color(black)  )) /// 
	(soybeans_phdi_`y'_w, label(Temp. and Precip. Controls) offset(0) ciopts(color(black) ) msymbol(T)) /// 
	(soybeans_phdi_`y'_h, label(Temp. and Wetness Controls) offset(0.2) ciopts(color(black)) msymbol(S)) ///
	, bylabel(Soybeans) ||, ///
	mcolor(black) mfcolor(white) vertical keep(*i_b *i_p *i_g) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(pdsi_* = "PDSI" pmdi_* = "PMDI" phdi_* = "PHDI")  ytitle("ln(`y_name')") ylab(,labs(small)) ///
	xlabel(1 "Pre-planting" 2 "Planting" 3 "Growing" 5 "Pre-planting" 6 "Planting" 7 "Growing" 9 "Pre-planting" 10 "Planting" 11 "Growing", labs(small))		
graph export "$root/results_replication/appendix_figures/afig7_`order'_alt_`y'.png", as(png) name("Graph") width(3200) height(2400) replace
}